package com.ybkj.daijia.api.weixin.handler.impl;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.WxSetting;
import com.ybkj.daijia.api.weixin.WxMsgType;
import com.ybkj.daijia.api.weixin.entity.WxBaseMsgEntity;
import com.ybkj.daijia.api.weixin.entity.WxBaseRespEntity;
import com.ybkj.daijia.api.weixin.entity.WxMsgEventEntity;
import com.ybkj.daijia.api.weixin.entity.WxRespTextEntity;
import java.util.Date;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class WxSubscribeEventHandler extends AbstractWxEventHandler {

    @Autowired
    private SettingUtils settingUtils;


    @Override
    public boolean canHandle(WxBaseMsgEntity msg) {
        if (super.canHandle(msg)) {
            if ("subscribe".equals(((WxMsgEventEntity) msg).getEvent().toLowerCase())) {
                return true;
            }
        }

        return false;
    }


    @Override
    public WxBaseRespEntity handleEvent(WxMsgEventEntity msg, Map<String, Object> context,
        WxBaseRespEntity resp) {

        WxSetting setting = settingUtils.getWx();

        //订阅消息
        WxRespTextEntity text = new WxRespTextEntity();
        text.setContent(setting.getWxSubscribe());
        text.setCreatedDate(new Date());
        text.setCreateTime(text.getCreatedDate().getTime());
        text.setFromUserName(msg.getToUserName());
        text.setToUserName(msg.getFromUserName());
        text.setFuncFlag(0);
        text.setMsgType(WxMsgType.text.name());

        return text;
    }

}
